package com.tender;

import com.tender.utils.ESOperationTemplate;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class A_10_QueryAllDoc {

    public static void main(String[] args) throws Exception {
        ESOperationTemplate.process(client -> {

            try {
                // 创建搜索请求对象
                SearchRequest request = new SearchRequest();
                request.indices("user2");
                // 构建查询的请求体
                SearchSourceBuilder builder = new SearchSourceBuilder();
                // 查询所有
                builder.query(QueryBuilders.matchAllQuery());

                request.source(builder);
                SearchResponse response = client.search(request, RequestOptions.DEFAULT);

                // 查询匹配
                SearchHits hits = response.getHits();
                System.out.println("took:" + response.getTook());
                System.out.println("timeout:" + response.isTimedOut());
                System.out.println("total:" + hits.getTotalHits());
                System.out.println("MaxScore:" + hits.getMaxScore());
                System.out.println("hits========>>");
                for (SearchHit hit : hits) {
                    //输出每条查询的结果信息
                    System.out.println(hit.getSourceAsString());
                }
                System.out.println("<<========");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return null;
        });
    }

}
